package com.modules.controller.sys;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.controller.MyController;
import com.common.log.LogData;
import com.common.vo.LoginUser;
import com.common.vo.Result;
import com.modules.entity.sys.UserEntity;
import com.modules.service.sys.OrganizationService;
import com.modules.service.sys.UserService;

import cn.hutool.core.util.StrUtil;


/**
 * 登录
 * @author yyq
 *
 */
@Controller
public class LoginController extends MyController<UserEntity>{

	@Autowired
	private UserService userService;
	@Autowired
	private OrganizationService organizationService;

	
	/**TODO
	 * 跳转登录和跳转主页、路径跳转
	 * @param a
	 * @return
	 */
	@RequestMapping(value="/login")
	public ModelAndView login(){
		return new ModelAndView("/login");
	}
	@RequestMapping(value="/jumpMain")
	public ModelAndView index(){
		return new ModelAndView("/main");
	}
	@RequestMapping(value="views/{a}")
	public ModelAndView commonsUrl(@PathVariable("a") String a){
		return new ModelAndView("/"+a);
	}
	@RequestMapping(value="views/{a}/{b}")
	public ModelAndView commonsUrl2(@PathVariable("a") String a,@PathVariable("b") String b){
		return new ModelAndView("/"+a+"/"+b);
	}
	@RequestMapping(value="views/{a}/{b}/{c}")
	public ModelAndView commonsUrl3(@PathVariable("a") String a,@PathVariable("b") String b,@PathVariable("c") String c){
		return new ModelAndView("/"+a+"/"+b+"/"+c);
	}
	
	/**
	 * 登录
	 * @return
	 */
	@LogData(module = "登录操作", remarks = "成功登录")
	@RequestMapping(value="/loginJudge")
	@ResponseBody
	public Result<?> loginJudge(UserEntity user){
		return userService.selectLogin(user);
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@RequestMapping(value="/loginExit")
	public ModelAndView loginExit(){
		removeUser();
		return new ModelAndView("/login");
	}
	
	/**
	 * 页面主题
	 * @return
	 */
	@LogData(module = "个人中心", remarks = "修改主题")
	@RequestMapping(value="/main/theme")
	@ResponseBody
	public Result<?> theme(UserEntity user){
		user.setId(getUser().getId());
		userService.updateById(user);
		if(StrUtil.isNotBlank(user.getTheme())) {
			setSessionValue("theme", user.getTheme());
			String ts = (String) getSessionValue("themeStyle");
			String module = "module_theme0";
			if(StrUtil.isNotBlank(ts)) {
				if(ts.indexOf("module1") != -1) {
             		module = "module_"+user.getTheme();
             	}
			}
			setSessionValue("moduleTheme", module);
		}
		if(StrUtil.isNotBlank(user.getThemeStyle())) {
			setSessionValue("themeStyle", user.getThemeStyle());
			String module = "module_theme0";
			if(StrUtil.isNotBlank(user.getThemeStyle())) {
				if(user.getThemeStyle().indexOf("module1") != -1) {
             		module = "module_"+getSessionValue("theme");
             	}
			}
			setSessionValue("moduleTheme", module);
		}
		return Result.success();
	}
	
	/**TODO
	 * 锁屏
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/main/suoping")
	@ResponseBody
	public Result<?> suoPing(String suoping, String mark){
		UserEntity user = new UserEntity();
		user.setPassword(suoping);
		user.setUserName(getUser().getUserName());
		return userService.suoPing(mark, user);
	}
	
	/**
	 * 个人信息
	 * @return
	 */
	@LogData(module = "个人中心", remarks = "修改个人信息")
	@RequestMapping(value="/main/basicData")
	@ResponseBody
	public Result<?> basicData(HttpServletRequest request){
		UserEntity byId = userService.getById(getUser().getId());
		return Result.success(byId);
	}
	
	/**
	 * 清除缓存
	 * @return
	 */
	@LogData(module = "个人中心", remarks = "清除缓存")
	@RequestMapping(value="/main/clearCache")
	@ResponseBody
	public Result<?> clearCache(HttpServletRequest request){
		
//		redisUtil.clear();
		
		return Result.success("清除成功");
	}
	
	/**
	 * 获取下面的所有项目
	 * @return
	 */
	@RequestMapping(value="/main/getAllOrg")
	@ResponseBody
	public Result<?> getAllOrg(HttpServletRequest request){
		return organizationService.select(getUser().getOid());
	}
	
	/**
	 * 切换组织
	 * @return
	 */
	@RequestMapping(value="/main/switchOrg")
	@ResponseBody
	public Result<?> switchOrg(Integer oid){
		LoginUser loginUser = getUser();
		loginUser.setLastOid(oid);
		setSessionValue("loginUser", loginUser);
		UserEntity user = new UserEntity();
    	user.setLastOid(oid);
    	user.setId(loginUser.getId());
		userService.updateById(user);
		return Result.success("切换成功");
	}
	
	
	
	

}
